package com.yjhb.android.sbz.activity

import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.adapter.CommentAdapter
import com.yjhb.android.sbz.entity.Comment
import com.yjhb.android.sbz.mvp.comment.LoadCommentPresenter
import com.yjhb.android.sbz.mvp.comment.LoadCommentView
import com.jojo.android.zxlib.tool.ZxToast
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener
import kotlinx.android.synthetic.main.activity_running_user_list.*


/**
 *  创建者 zx
 *  创建时间 2021/3/23 16:11
 */
class MoreCommentListActivity : BaseActivity<LoadCommentPresenter>(), LoadCommentView, OnRefreshLoadmoreListener {

    private val adapter = CommentAdapter(this)
    private val data = mutableListOf<Comment>()
    private val size = 25
    private var current = 1
    private var taskId: String? = null

    override fun initPresenter(): LoadCommentPresenter {
        return LoadCommentPresenter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.layout_more_comment
    }

    override fun initUI() {
        taskId = intent.getStringExtra("taskId")
        emptyRecyclerView.emptyView = layout_empty
        emptyRecyclerView.adapter = adapter
        adapter.setComments(this.data)
    }

    override fun initData() {
        loadingDialog.setTipsText("正在加载...")
        loadingDialog.show()
        load(false)
    }

    override fun regEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(this)
        back.setOnClickListener { finish() }
    }

    private fun load(isLoadMore: Boolean) {
        if (isLoadMore) {
            ++current
        } else {
            current = 1
        }
        presenter.commentList(taskId, size, current)
    }

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        load(true)
    }

    override fun onRefresh(refreshlayout: RefreshLayout?) {
        load(false)
    }

    override fun onLoad(data: List<Comment>?, isRefresh: Boolean) {
        smartRefreshLayout.finishLoadmore()
        smartRefreshLayout.finishRefresh()
        loadingDialog.cancel()
        if (isRefresh) {
            this.data.clear()
        }
        data?.let {
            this.data.addAll(it)
        }
        adapter.notifyDataSetChanged()
    }

    override fun onFailure(code: Int, msg: String?) {
        loadingDialog.cancel()
        ZxToast.showINFOBottomToast(msg)
    }
}